﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;

namespace wesiGestClinica.Shared
{
    public class SiteMapProviderSGM : SiteMapProvider
    {
         private SiteMapNode rootNode = null;
    private ArrayList siteMapNodes = null;
    private ArrayList childParentRelationship = null;

    public SiteMapProviderSGM()
    {
        siteMapNodes = new ArrayList();
        childParentRelationship = new ArrayList();

    }
    public override SiteMapNode RootNode
    {
        get
        {
            return rootNode;
        }
    }
    private SiteMapNode GetNode(ArrayList list, string key)
    {
        for (int i = 0; i < list.Count; i++)
        {
            DictionaryEntry item = (DictionaryEntry)list[i];
            if ((string)item.Key == key)
                return item.Value as SiteMapNode;
        }
        return null;
    }
    public void AgregarRaiz(string urlRoot, string nombreRoot, string key)
    {
        if (rootNode != null)
        {
            return;
        }
        else
        {
            rootNode = null;
            siteMapNodes.Clear();
            childParentRelationship.Clear();
            SiteMapNode temp = null;
            temp = new SiteMapNode(this, key);
            temp.Description = nombreRoot;
            if (urlRoot != null)
            {
                //temp.Url = HttpRuntime.AppDomainAppVirtualPath + "/" + urlRoot ;
                temp.Url = urlRoot;
            }
            temp.Title = nombreRoot;
            rootNode = temp;
        }
        return;
    }
    public SiteMapNode AgregarNodo(SiteMapNode nodopadre, string urlNodo, string nombreNodo, string key)
    {
        SiteMapNode temp = null;
        // Construir los nodos SiteMapNode y agregarlos al ArrayList collections. 
        temp = new SiteMapNode(this, key);
        
        temp.Description = nombreNodo;
        if (urlNodo != null)
        {
            //temp.Url = HttpRuntime.AppDomainAppVirtualPath + "/" + urlNodo ;
            temp.Url = urlNodo;
        }
        temp.Title = nombreNodo;

        siteMapNodes.Add(new DictionaryEntry(temp.Key, temp));
        // El nodo padre ha sido ya agregado a la coleccion.
        SiteMapNode parentNode = FindSiteMapNode(nodopadre.Key);
        if (parentNode != null)
        {
            childParentRelationship.Add(new DictionaryEntry(temp.Key, parentNode));
        }
        else
        {
            throw new Exception("Parent node not found for current node.");
        }
        
        return temp;
    }

    #region metodos de la interfaz
    public override SiteMapNode FindSiteMapNode(string rawKey)
    {
        if (RootNode.Key == rawKey)
        {
            return RootNode;
        }
        else
        {
            SiteMapNode candidate = null;
            lock (this)
            {
                candidate = GetNode(siteMapNodes, rawKey);
            }
            return candidate;
        }
    }
    public override SiteMapNodeCollection GetChildNodes(SiteMapNode node)
    {
        SiteMapNodeCollection children = new SiteMapNodeCollection();
        lock (this)
        {
            for (int i = 0; i < childParentRelationship.Count; i++)
            {

                string nodeUrl = ((DictionaryEntry)childParentRelationship[i]).Key as string;
                SiteMapNode parent = GetNode(childParentRelationship, nodeUrl);
                if (parent != null && node.Url == parent.Url)
                {
                    SiteMapNode child = FindSiteMapNode(nodeUrl);
                    if (child != null)
                    {
                        children.Add(child as SiteMapNode);
                    }
                    else
                    {
                        throw new Exception("ArrayLists not in sync.");
                    }
                }
            }
        }
        return children;
    }
    public override SiteMapNode GetParentNode(SiteMapNode node)
    {
        SiteMapNode parent = null;
        lock (this)
        {
            parent = GetNode(childParentRelationship, node.Url);
        }
        return parent;
    }
    protected override SiteMapNode GetRootNodeCore()
    {
        return RootNode;
    }
    #endregion
    }
}
